$(function () {
    // 获取首页信息
    var loading = weui.loading('loading', {className: 'custom-classname'});
    // 获取最新列表
    var result = Model
        .gift
        .myGift({index: 0, size: 0});
    if (result.code) {
        Common
            .fn
            .formatOutput(result);
        loading.hide();
        return false;
    }
    // 遍历礼品
    for (var i = 0; i < result.data.length; i++) {
        var res = result.data[i];
        var item = '<a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg inte-index-list" data-id="' + res.id + '"><div class="weui-media-box__hd"><img class="weui-media-box__thumb lazy-img" data-original="' + res.picture + '" alt=""></div><div class="weui-media-box__bd"><h4 class="weui-media-box__title">' + res.name + '</h4><p class="weui-media-box__desc">';
        if(res.free == '不包邮') {
            item += '邮费 ' + res.postage;
        }else {
			item += res.free;
		}
        item += '</p><p class="weui-media-box__desc">剩余：' + res.number + '</p></div><div><div class="inte-index-list-price inte-index-list-price2">' + res.price + '红米</div><button class="inte-list-btn bg-ff5454 inte-del-btn" href="javascript:;" data-id="' + res.id + '">删除</button><button class="inte-list-btn bg-f5a623 inte-edit-btn" href="edit_gift.html?id=' + res.id + '" data-id="' + res.id + '">编辑</button></div></a>';
        $('.inte-mygift-lists').append(item);
    }
    // 图片懒加载
    lazyImg();
    loading.hide();
});

// 图片懒加载
function lazyImg() {
	$('.lazy-img').lazyload({
		effect: 'fadeIn',
	});
}

// 商品详情
$(document).on('click', '.inte-index-list', function(e) {
    var id = $(this).attr('data-id');
    location.href = 'gift_detail.html?id=' + id;
    e.stopPropagation();
});

// 删除商品
$(document).on('mousedown', '.inte-del-btn', function() {
    var id = $(this).attr('data-id'),
        confirmDom = weui.confirm('确认要删除该商品吗？', {
		title: '系统提示',
		buttons: [
			{
				label: '取消',
				type: 'default',
				onClick: function() {
					console.log('no');
				},
			},
			{
				label: '确认',
				type: 'primary',
				onClick: function() {
					var result = Model.gift.delGift({
						giftId: id
					});
					if (result.code) {
						alert(result.message);
						confirmDom.hide();
						return false;
					}
					alert(result.data);
					location.reload();
				},
			},
		],
	});
});

// 编辑商品
$(document).on('mousedown', '.inte-edit-btn', function() {
    var id = $(this).attr('data-id');
    weui.confirm(
		'请输入当前商品库存<input type="number" id="toPrice" style="border: 0;font-size: 3rem;text-align: center;width: 100%;border-bottom: 1px solid #000;">',
		{
			title: '系统提示',
			buttons: [
				{
					label: '取消',
					type: 'default',
					onClick: function() {
						console.log('no');
					},
				},
				{
					label: '确认修改',
					type: 'primary',
					onClick: function() {
						// 判断出价是否高于起降价
						// 判断输入值是否合法
						var regu = '^[0-9]*$',
							re = new RegExp(regu),
							s = $.trim($('#toPrice').val());
						if (re.test(s)) {
							// 输入合法
							var loading = weui.loading('loading', {
								className: 'custom-classname',
							});
							var result = Model.gift.editGift({
								giftId: id,
								number: $('#toPrice').val()
                            });
                            if (result.code) {
								alert(result.message);
								confirmDom.hide();
								return false;
							}
                            alert(result.data);
                            location.reload();
							loading.hide();
						} else {
							// 输入不合法
							alert('输入数量有误，请重新输入。');
							return false;
						}
					},
				},
			],
		}
	);
});